/* *
 * MY Script for Oneway
 * @author: lethanhquang910@gmail.com
 * */

/**
 * Get block home video
 */
function getAjaxHomeVideo()
{
    $.ajax({
        type: 'GET',
        url: site_url + '/video/getBlockHomeVideo',
        data:{},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#block_video_content').html(data).fadeIn();
        }
    });

    return false;
}

/**
 * Get block home audio
 */
function getAjaxHomeAudio()
{
    $.ajax({
        type: 'GET',
        url: site_url + '/audio/getBlockHomeAudio',
        data:{},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#block_audio_content').html(data).fadeIn();
        }
    });

    return false;
}

/**
 * Get block home consulting
 */
function getAjaxHomeConsulting()
{
    $.ajax({
        type: 'GET',
        url: site_url + '/consulting/getBlockHomeConsulting',
        data:{},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#block_consulting_content').html(data).fadeIn();

            //Get home Question
            getAjaxHomeQuestion();
        }
    });

    return false;
}

/**
 * Get block home consulting
 */
function getAjaxHomeQuestion()
{
    $.ajax({
        type: 'GET',
        url: site_url + '/consulting/getBlockHomeQuestion',
        data:{},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#home-consulting-question').html(data).fadeIn();
        }
    });

    return false;
}

/**
 * Get block home consulting
 */
function getAjaxHomeNews()
{
    $.ajax({
        type: 'GET',
        url: site_url + '/news/getBlockHomeNews',
        data:{},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#news_left_block').html(data).fadeIn();

            //NEWSTICKER BOX
            $("#newsticker").jCarouselLite({
                vertical: true,
                visible: 10,
                auto: true,
                speed: 2000
            });
        }
    });

    return false;
}

/**
 * Load list node older by nodeID
 */
function getListNodeOlder(nodeID, contentID, link)
{
    $.ajax({
        type: 'GET',
        url: site_url + '/news/getListNodeOlder',
        data:{nodeID:nodeID,link:link},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#'+contentID).html(data).fadeIn();
        }
    });

    return false;
}

/**
 * share link facebook
 **/
function facebook_share()
{
    var left = (screen.width/2)-313;
    var top = (screen.height/2)-218;
    var u = location.href;
    var t = document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer',',width=626,height=436,left='+left+',top='+top);
    return false;
}

/**
 * share link twitter
 **/
function twitter_share()
{
    var left = (screen.width/2)-313;
    var top = (screen.height/2)-218;
    var u = location.href;
    var t = document.title;
    window.open('http://twitter.com/share?url='+encodeURIComponent(u)+'&text='+encodeURIComponent(t),'sharer',',width=626,height=436,left='+left+',top='+top);
    return false;
}

/**
 * Get random video
 */
function getRandVideo(categoryID, limit, prefix)
{
    $.ajax({
        type: 'GET',
        url: site_url+'/video/getRandVideo',
        data:{categoryID:categoryID,limit:limit},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#'+prefix).html(data).fadeIn();

            //Video gallery
            $("ul#video_gallery").carouFredSel({
                auto: true,
                prev: "#"+prefix+"_prev",
                next: "#"+prefix+"_next"
            });
        }
    });

    return false;
}

/**
 * Get comment
 */
function getComment(nodeID, limit, offset)
{
     $.ajax({
        type: 'GET',
        url: site_url+'/comment/getListComment',
        data:{nodeID:nodeID,limit:limit,offset:offset},
        dataType: 'html',
        success: function(data)
        {
            //Addpend data to HTML
            $('#comment-box').html(data).fadeIn();
        }
    });

    return false;
}

/**
 * Add new comment
 */
function addComment(nodeID)
{
    //Get data
    var name = $('#cm_name').val();
    var email = $('#cm_email').val();
    var content = $('#cm_content').val();

    //Check data
    if(name == '' || content == '')
    {
        alert('Vui lòng điền đầy đủ thông tin trước khi bình luận.');
        return false;
    }

    if(content.length > 500)
    {
        alert('Nội dung bình luận không được quá 500 ký tự.');
        return false;
    }

    $.ajax({
        type: 'POST',
        url: site_url+'/comment/insertComment',
        data:{nodeID:nodeID,name:name,email:email,content:content},
        dataType: 'json',
        success: function(response)
        {
            if(response.commentID > 0)
            {
                //show message
                $('#comment-form').fadeOut('fast', function(){
                    $(this).html('<div class="success center">Bình luận của bạn đã được gởi thành công và đang chờ BQT oneway xét duyệt.</div>').fadeIn('fast');
                });
            }
            else
            {
                alert('Có lỗi xảy ra, vui lòng thử lại.');
            }
        }
    });

    return false;
}

/**
 * Get list hot node
 */
function getHotNode(limit)
{
    $.ajax({
        type: 'GET',
        url: site_url+'/news/getListHotNode',
        data:{limit:limit},
        dataType: 'html',
        success: function(response)
        {
            //Append html
            $('#hot_node_block').html(response).fadeIn();
        }
    });

    return false;
}

function getAjaxHomeGallery()
{
    $.ajax({
        type: 'GET',
        url: site_url+'/event/getListEvent',
        data:{},
        dataType: 'html',
        success: function(response)
        {
            //Append html
            $('#block_gallery_content').html(response).fadeIn();

            // GALLERY - HOME PAGE
            $.featureList(
                $("#tabs_list li a"),
                $("#output li"), {start_item:1}
            );
        }
    });

    return false;
}

/**
 * Get list hot video by categoryId
 */
function getHotVideo(category, categoryID, limit, offset, prefix)
{
    $.ajax({
        type: 'GET',
        url: site_url+'/video/getHotVideo',
        data:{category:category,categoryID:categoryID,limit:limit,offset:offset},
        dataType: 'html',
        success: function(response)
        {
            //Append html
            $('#'+prefix).html(response);
        }
    });

    return false;
}

/**
 * Get list hot video by categoryId
 */
function getNewVideo(category, categoryID, page, limit, prefix)
{
    $.ajax({
        type: 'GET',
        url: site_url+'/video/getNewVideo',
        data:{category:category,categoryID:categoryID,limit:limit,page:page,prefix:prefix},
        dataType: 'html',
        success: function(response)
        {
            //Append html
            $('#'+prefix).html(response);
        }
    });

    return false;
}

/**
 * ask question ajax
 */
function askQuestion()
{
    //Get params
    var fullname = $('#sender-name').val();
    var sender = $('#sender-email').val();
    var content = $('#email-content').val();
    
    $('#loading').fadeIn();
    $('#error_message').hide();

    $.ajax({
        type: 'POST',
        url: site_url+'/consulting/askQuestion',
        data:{sender:sender,fullname:fullname,content:content},
        dataType: 'json',
        success: function(response)
        {
            $('#loading').hide();
             
            if(response.error == 0)
            {
                $('#sender-name').val('');
                $('#sender-email').val('');
                $('#email-content').val('');
                $('#error_message').text('Câu hỏi của bạn đã được gởi đi.').css('color','#40ac05').fadeIn();
                return false;
            }
            else
            {
                $('#error_message').text(response.message).css('color','#C00').fadeIn();
                return false;
            }
        }
    });
    return false;
}

/**
 * Send contact ajax
 */
function sendContact()
{
    //Get params
    var fullname = $('#sender-name').val();
    var sender = $('#sender-email').val();
    var address = $('#sender-address').val();
    var mobile = $('#sender-mobile').val();
    var age = $('#sender-age').val();
    var job = $('#sender-job').val();
    var title = $('#email-title').val();
    var content = $('#email-content').val();
    
    $('#loading').fadeIn();
    $('#error_message').hide();

    $.ajax({
        type: 'POST',
        url: site_url+'/contact/sendContact',
        data:{sender:sender,fullname:fullname,content:content,address:address,mobile:mobile,age:age,job:job,title:title},
        dataType: 'json',
        success: function(response)
        {
            $('#loading').hide();
            if(response.error == 0)
            {
                $('#contact-form').hide();
                $('#contact-message').fadeIn();
                return false;
            }
            else
            {
                $('#error_message').text(response.message).css('color','#C00').fadeIn();
                return false;
            }
        }
    });
    return false;
}

/**
 * Get advertising
 */
function getAdvertising(type, limit)
{
    $.ajax({
        type: 'GET',
        url: site_url+'/home/advertising',
        data:{type:type,limit:limit},
        dataType: 'json',
        success: function(response)
        {
            if(response != '')
            {
                var $a, i;
                
                for(i in response)
                {
                    $a = $('<a target="_blank"></a>').attr('href', response[i].link).html(response[i].value);
                    $('#adv-'+type).append($a);
                }
                
                $('#adv-'+type).show();
            }
        }
    });
    
    return false;
}

$(document).ready(function() {

    //Menu drop down
    $('#menu > ul').dropmenu({nbsp:true});
});